package meta.api.web.common.presentation;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import meta.api.web.common.util.ApiUtil;
import meta.common.msg.MessageConst;
import meta.common.msg.MsgResourceManager;
import meta.common.util.StrUtil;

@Controller
public class GlobalController {

    @RequestMapping(value="/error.json")
    public String error(HttpServletRequest req, HttpServletResponse res) throws Exception {
        HttpSession session = req.getSession(false);
        if (session != null) {
            session.invalidate();
        }
        String errorMsg = (String) req.getAttribute(GlobalErrorAdvice.ERROR_MSG);
        errorMsg = StrUtil.isEmpty(errorMsg)
                ? MsgResourceManager.getMessage(MessageConst.MSG_01090001)
                : errorMsg;
        ApiUtil.setErrorJsonResponse(res, errorMsg);
        return null;
    }
}
